package com.smag.quartz.runner;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smag.common.constant.SystemConstants;
import com.smag.common.core.domain.entity.FarmArticle;
import com.smag.common.core.redis.RedisCache;
import com.smag.system.service.FarmArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class ViewCountRunner implements CommandLineRunner {

    @Autowired
    private FarmArticleService farmArticleService;

    @Autowired
    private RedisCache redisCache;

    @Override
    public void run(String... args) throws Exception {
        //获取文章id和文章的访问量
        LambdaQueryWrapper<FarmArticle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FarmArticle::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<FarmArticle> farmArticleList = farmArticleService.list(queryWrapper);
        Map<String, Integer> viewCountMap = farmArticleList.stream()
                .collect(Collectors.toMap(article -> article.getId().toString(), article -> article.getViewCount().intValue()));
        redisCache.setCacheMap(SystemConstants.VIEW_COUNT,viewCountMap);
    }
}